package com.credithc.customers.base.utils;

import com.alibaba.fastjson.JSONObject;
import com.credithc.customers.base.ro.EmailSendRo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AlarmUtils {
    private static final String MAIL_KEY = "3ef9cd94f447cc1724e46393cadd6836d04a5979";
    private static final String MAIL_URL = "http://10.10.255.196/api/v1/email";
    private static final String DEFAULY_RECEIVER = "denghuixiang180601@credithc.com";

    //    @Value("${mail.send.key}")
//    public void setMailKey(String key) {
//        AlarmUtils.mailKey = key;
//    }
//
//    @Value("${mail.send.url:http://10.10.255.196/api/v1/email}")
//    public void setMailUrl(String url) {
//        AlarmUtils.mailUrl = url;
//    }
    public static String sendMail(EmailSendRo ro) {
        String emailStatus = "1";
        ro.setKey(MAIL_KEY);
        ro.setBcc("");
        ro.setCc(null);
        if (StringUtils.isBlank(ro.getTo())) {
            ro.setTo(DEFAULY_RECEIVER);
        }
        String emailUrl = MAIL_URL;
        String param = JSONObject.toJSONString(ro);
        log.info("发送邮件预警信息----------------------" + param + emailUrl);
        try {
            String result = HttpClientUtil.postJSON(emailUrl, param, null, null);
            JSONObject jsStr = JSONObject.parseObject(result);
            //返回结果code为0，表示通知成功
            if ("0".equals(jsStr.getString("code"))) {
                emailStatus = "0";
            }
            log.info("发送邮件预警成功----------------------" + result);
        } catch (Exception e) {
            log.info("发送邮件预警失败----------------------" + e);
            e.printStackTrace();
        }
        return emailStatus;
    }


    public static void main(String[] args) {
//		JSONObject param1 = new JSONObject();
//		param1.put("key", "3ef9cd94f447cc1724e46393cadd6836d04a5979");
//		param1.put("to", "denghuixiang180601@credithc.com");
//		param1.put("cc", "");
//		param1.put("bcc", "");
//		param1.put("subject", "111111111");
//		param1.put("body", "11111111111");
//		String json = JSONObject.toJSONString(param1);
//		System.out.println(json);
        EmailSendRo ro = new EmailSendRo();
        ro.setBody("邮件通知test");
        ro.setTo("denghuixiang180601@credithc.com");
        ro.setSubject("yzt邮件通知");
        AlarmUtils.sendMail(ro);
        System.out.println("111111111111111111");
    }
}
